package com.nanhai.buddhist.college.module.study.service.studyadditionalcourses;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyadditionalcourses.StudyAdditionalCoursesClassDO;
import com.nanhai.buddhist.college.module.study.dal.mysql.studyadditionalcourses.StudyAdditionalCoursesClassMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.nanhai.buddhist.college.module.study.controller.admin.studyadditionalcourses.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyadditionalcourses.StudyAdditionalCoursesDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.study.convert.studyadditionalcourses.StudyAdditionalCoursesConvert;
import com.nanhai.buddhist.college.module.study.dal.mysql.studyadditionalcourses.StudyAdditionalCoursesMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 选课管理 Service 实现类
 *
 * @author xqenzo@163.com
 */
@Service
@Validated
public class StudyAdditionalCoursesServiceImpl implements StudyAdditionalCoursesService {

    @Resource
    private StudyAdditionalCoursesMapper additionalCoursesMapper;

    @Resource
    private StudyAdditionalCoursesClassMapper studyAdditionalCoursesClassMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String createAdditionalCourses(StudyAdditionalCoursesCreateReqVO createReqVO) {
        // 插入
        StudyAdditionalCoursesDO additionalCourses = StudyAdditionalCoursesConvert.INSTANCE.convert(createReqVO);
        setProfessionStr(createReqVO, additionalCourses);
        additionalCoursesMapper.insert(additionalCourses);

        // 插入 课程和班级信息表
        List<StudyAdditionalCoursesClassDO> collect = createReqVO.getClassesIds().stream().map(s -> StudyAdditionalCoursesClassDO.builder().classesId(s).studyAdditionalCoursesId(additionalCourses.getId()).build()).collect(Collectors.toList());
        studyAdditionalCoursesClassMapper.insertBatch(collect);
        // 返回
        return additionalCourses.getId();
    }

    /**
     * 设置专业id
     * @param createReqVO
     * @param additionalCourses
     */
    private void setProfessionStr(StudyAdditionalCoursesCreateReqVO createReqVO, StudyAdditionalCoursesDO additionalCourses) {
        if (CollectionUtils.isNotEmpty(createReqVO.getProfessionIds())) {
            StringBuffer stringBuffer = new StringBuffer();
            final List<String> professionIds = createReqVO.getProfessionIds();
            for (int i = 0; i < professionIds.size(); i++) {
                stringBuffer.append(professionIds.get(i));
                if (i == professionIds.size()-1){
                    break;
                }
                stringBuffer.append(",");
            }
            additionalCourses.setProfessionStr(stringBuffer.toString());
        }
    }


    @Override
    public void updateAdditionalCourses(StudyAdditionalCoursesUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateAdditionalCoursesExists(updateReqVO.getId());
        // 更新
        StudyAdditionalCoursesDO updateObj = StudyAdditionalCoursesConvert.INSTANCE.convert(updateReqVO);
        additionalCoursesMapper.updateById(updateObj);

        // 删除 课程和班级信息表
        LambdaQueryWrapperX<StudyAdditionalCoursesClassDO> eq = new LambdaQueryWrapperX<StudyAdditionalCoursesClassDO>().eq(StudyAdditionalCoursesClassDO::getStudyAdditionalCoursesId, updateReqVO.getId());
        studyAdditionalCoursesClassMapper.delete(eq);

        // 插入 课程和班级信息表
        List<StudyAdditionalCoursesClassDO> collect = updateReqVO.getClassesIds().stream().map(s -> StudyAdditionalCoursesClassDO.builder().classesId(s).studyAdditionalCoursesId(updateReqVO.getId()).build()).collect(Collectors.toList());
        studyAdditionalCoursesClassMapper.insertBatch(collect);

    }

    @Override
    public void deleteAdditionalCourses(String id) {
        // 校验存在
        this.validateAdditionalCoursesExists(id);
        // 删除
        additionalCoursesMapper.deleteById(id);

        LambdaQueryWrapperX<StudyAdditionalCoursesClassDO> eq = new LambdaQueryWrapperX<StudyAdditionalCoursesClassDO>().eq(StudyAdditionalCoursesClassDO::getStudyAdditionalCoursesId, id);
        studyAdditionalCoursesClassMapper.delete(eq);
    }

    private void validateAdditionalCoursesExists(String id) {
        if (additionalCoursesMapper.selectById(id) == null) {
            throw exception(ADDITIONAL_COURSES_NOT_EXISTS);
        }
    }

    @Override
    public StudyAdditionalCoursesDO getAdditionalCourses(String id) {
        return additionalCoursesMapper.selectByIdDeep(id);
    }

    @Override
    public List<StudyAdditionalCoursesDO> getAdditionalCoursesList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : additionalCoursesMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudyAdditionalCoursesDO> getAdditionalCoursesPage(StudyAdditionalCoursesPageReqVO pageReqVO) {
        return additionalCoursesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudyAdditionalCoursesDO> getAdditionalCoursesList(StudyAdditionalCoursesExportReqVO exportReqVO) {
        return additionalCoursesMapper.selectList(exportReqVO);
    }

}
